# -*- coding: utf-8 -*-
# #############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields


class sale_order(osv.osv):
    _inherit = 'sale.order'

    def _get_invoice_date(self, cr, uid, ids, name, attr, context=None):
        res = {}
        for sale_id in self.browse(cr, uid, ids):
            list_invoice = sale_id.invoice_ids
            res.update({
                sale_id.id: False
            })
            if list_invoice:
                id_invoice_first = list_invoice[0]
                res.update({
                    sale_id.id: id_invoice_first.date_invoice
                })
        return res

    _columns = {
        'first_invoice_date': fields.function(_get_invoice_date, string="Invoice Date", method=True, type='date',
                                              select=True, store=True),
    }


sale_order()


class res_partner(osv.osv):
    _inherit = 'res.partner'

    def _get_partner_categ(self, cr, uid, ids, name, attr, context=None):
        res = {}
        for partner_id in self.browse(cr, uid, ids):
            list_categ = partner_id.category_id
            res.update({
                partner_id.id: False
            })
            if list_categ:
                id_categ_first = list_categ[0]
                res.update({
                    partner_id.id: id_categ_first.id
                })
        return res

    _columns = {
        'first_partner_categ': fields.function(_get_partner_categ, relation='res.partner.category', method=True,
                                               string="Partner Categ", type='many2one', select=True, store=True),
    }


res_partner()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
